package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewAccountActionServlet extends HttpServlet {
    public static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";
    public static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    public static final String VIEW_NEW_ACCOUNT_FORM = "/WEB-INF/jsp/account/NewAccountForm.jsp";
    private String username;
    private String password;
    private String repeatedPassword;
    private Account account;
    private AccountService accountService;
    private boolean authenticated = false;
    private boolean optionCheck;
    private boolean bannerCheck;
    private LogService logService;
    private String imageText;
    private String text;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        account = new Account();
        HttpSession session = request.getSession();
        //imageText = request.getParameter("image");
        //text = (String) session.getAttribute("text");
        /*if (!text.equalsIgnoreCase(imageText)){
            request.setAttribute("imageMess", "Invalid Code!");
            logService = new LogService();
            logService.insertLog("Invalid VerifyCode","Invalid VerifyCode!", "null");
            request.getRequestDispatcher(VIEW_NEW_ACCOUNT_FORM).forward(request,response);
        }
        else */
            {
            accountService = new AccountService();
            username = request.getParameter("username");
            if (accountService.getAccount(username) != null) {
                session.setAttribute("message", "Username already exists!");
                request.getRequestDispatcher(ERROR).forward(request, response);
            } else {
                account.setUsername(username);
                password = request.getParameter("password");
                repeatedPassword = request.getParameter("repeatedPassword");
                if (!password.equals(repeatedPassword)) {
                    session.setAttribute("message", "Entered passwords differ.");
                    request.getRequestDispatcher(ERROR).forward(request, response);
                } else {
                    account.setPassword(password);
                    account.setFirstName(request.getParameter("firstName"));
                    account.setLastName(request.getParameter("lastName"));
                    account.setEmail(request.getParameter("email"));
                    account.setPhone(request.getParameter("phone"));
                    account.setAddress1(request.getParameter("address1"));
                    account.setAddress2(request.getParameter("address2"));
                    account.setCity(request.getParameter("city"));
                    account.setState(request.getParameter("state"));
                    account.setZip(request.getParameter("zip"));
                    account.setCountry(request.getParameter("country"));
                    account.setLanguagePreference(request.getParameter("languagePreference"));
                    account.setFavouriteCategoryId(request.getParameter("favouriteCategoryId"));

                    if (request.getParameter("listOption") == null){
                        optionCheck = false;}
                    else {
                        optionCheck = true;
                    }
                    if (request.getParameter("bannerOption") == null){
                        bannerCheck = false;}
                    else {
                        bannerCheck = true;
                    }
                    account.setListOption(optionCheck);
                    account.setBannerOption(bannerCheck);

                    accountService.insertAccount(account);
                    authenticated = true;
                    session.setAttribute("account", account);
                    session.setAttribute("authenticated", authenticated);
                    logService = new LogService();
                    logService.insertLog("New Account","Username:"+account.getUsername(), account.getUsername());
                    request.getRequestDispatcher(MAIN).forward(request, response);
                }
            }
        }
    }
}
